package com.wondertek.service.service.business.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wondertek.common.exception.AppException;
import com.wondertek.common.util.base.PoiUtil;
import com.wondertek.common.util.base.StringUtil;
import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.app.req.RentalListReq;
import com.wondertek.service.dto.app.res.RentalListExportRes;
import com.wondertek.service.dto.app.res.RentalListRes;
import com.wondertek.service.dto.business.req.RentalDeviceAddReq;
import com.wondertek.service.dto.business.req.RentalFindRdReq;
import com.wondertek.service.dto.business.req.RentalPostponeReq;
import com.wondertek.service.dto.business.req.RentalRoomDeviceReq;
import com.wondertek.service.dto.business.res.*;
import com.wondertek.service.entity.business.RentalDevicePo;
import com.wondertek.service.mapper.business.RentalMapper;
import com.wondertek.service.service.business.RentalService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 租赁管理
 *
 * @author wangyu
 */
@Service
public class RentalServiceImpl extends ServiceImpl<RentalMapper, RentalDevicePo> implements RentalService {

    @Autowired
    private RentalMapper rentalMapper;

    /**
     * 查询租赁管理
     *
     * @param req
     * @return
     */
    public Page<RentalListRes> findRentalList(RentalListReq req) {


        return rentalMapper.findRentalList(req, new Page<>(req.getPageNo(), req.getPageSize()));
    }

    /**
     * 租赁管理导出
     * @param req
     * @return
     */
    @Override
    public void findRentalListExport(RentalListReq req, HttpServletResponse response) {

        if (req.getIds() != null && !req.getIds().isEmpty()) {
            RentalListReq rentalListReq = new RentalListReq();
            rentalListReq.setIds(req.getIds());
            req = rentalListReq;
        }

        List<RentalListRes> rentalListExport = rentalMapper.findRentalListExport(req);


        List<RentalListExportRes> rentalListExportRes = BeanUtil.copyToList(rentalListExport, RentalListExportRes.class);

        PoiUtil.writeExcelToResponse("租赁管理", RentalListExportRes.class, rentalListExportRes, response);

    }

    /**
     * 查询分区租赁、设备租赁
     *
     * @return
     */
    public Page<RentalRoomDeviceRes> findRentalRoomDevice(RentalRoomDeviceReq req) {

        return rentalMapper.findRentalRoomDevice(req, new Page<>(req.getPageNo(), req.getPageSize()));
    }

    /**
     * 添加租赁信息
     *
     * @param req
     */
    public void inertRentalDevice(RentalDeviceAddReq req) {

        List<String> deviceId = req.getDeviceId();

        List<String> roomId = req.getRoomId();

        if (roomId != null && !roomId.isEmpty() && deviceId.size() != roomId.size())
            throw new AppException("参数错误:  device和room数组长度不一致");

//        if(rentalMapper.findRentalRoomDeviceCount(req) > 0)
//            throw new AppException("不能重复添加租赁!");

        List<RentalDevicePo> pos = new ArrayList<>();

        for (int i = 0; i < deviceId.size(); i++) {
            RentalDevicePo rentalDevicePo = new RentalDevicePo();
            rentalDevicePo.setIsDelete(0);
            rentalDevicePo.setDeviceId(deviceId.get(i));
            rentalDevicePo.setConsumerId(req.getConsumerId());
            rentalDevicePo.setStartDate(req.getStartDate());
            rentalDevicePo.setEndDate(req.getEndDate());

            if (roomId != null && !roomId.isEmpty()) {
                rentalDevicePo.setRoomId(roomId.get(i));
            }

            Integer rentalDeviceRoomCount = rentalMapper.findRentalDeviceRoomCount(rentalDevicePo.getConsumerId(), rentalDevicePo.getDeviceId(), rentalDevicePo.getRoomId());
            if(rentalDeviceRoomCount > 0)
                throw new AppException("不能重复添加租赁!");

            pos.add(rentalDevicePo);
        }

        //   RentalDevicePo rentalDevicePo = new RentalDevicePo();

        // BeanUtil.copyProperties(req,rentalDevicePo);


        this.saveBatch(pos);

    }

    /**
     * 查询所有点位
     * @param id 点位或设备id
     * @param type  1站点 2设备 3分区
     * @param rentalType 1:分区 2:设备
     * @return
     */
    public List<RentalKvRes> findAllSite(String id, Integer type, Integer rentalType) {
        switch (type){
            case 1:
                String userId = "";
                String str = SecurityUtil.getDefaultLoginUser().getUserId();
                if(!"1".equals(str)) userId = str;
                return rentalMapper.findAllSite(userId);
            case 2:
                if(StringUtil.isEmpty(id))
                    throw new AppException("id.not.null");
                if(rentalType == null || (rentalType != 1 && rentalType != 2))
                    throw new AppException("type.not.null");
                return rentalMapper.findDeviceBySiteId(id,rentalType);
            case 3:
                if(StringUtil.isEmpty(id))
                    throw new AppException("id.not.null");
                return rentalMapper.findRoomByDeviceId(id);
            default:
                throw new AppException("type.not.null");
        }

    }

    /**
     * 查询站点下分区/设备
     *
     * @param req
     * @return
     */
    public List<?> findAllRoomBySiteId(RentalFindRdReq req) {
        List<RoomAllRes> allRoomBySiteId = rentalMapper.findAllRoomBySiteId(req);
        
        if(req.getType() == 2) return allRoomBySiteId;
    //    Map<String> map = new HashMap<>();

        List<RoomAllRes> list = new ArrayList<>();
        List<DeviceRoomAllRes> resList = new ArrayList<>();

        String deviceId = "";
        String deviceName = "";

        for (int i = 0; i < allRoomBySiteId.size(); i++) {
            RoomAllRes roomAllRes = allRoomBySiteId.get(i);
            if(deviceId.equals("")) {
                deviceId = roomAllRes.getDeviceId();
                deviceName = roomAllRes.getDeviceName();
            }
            if(deviceId.equals(roomAllRes.getDeviceId())) {
                list.add(roomAllRes);
                allRoomBySiteId.remove(i);
                i--;
            }

            if(i == (allRoomBySiteId.size() - 1)){
                DeviceRoomAllRes deviceRoomAllRes = new DeviceRoomAllRes();
                deviceRoomAllRes.setDeviceName(deviceName);
                deviceRoomAllRes.setList(list);
                list =  new ArrayList<>();
                resList.add(deviceRoomAllRes);
            }


            if (i >= (allRoomBySiteId.size() - 1) && allRoomBySiteId.size() != 0){
                i = -1;
                deviceId = "";
            }


        }

        return resList;

    }

    /**
     * 延期
     *
     * @param req
     */
    @Override
    public void postpone(RentalPostponeReq req) {

        RentalDevicePo rentalDevicePo = new RentalDevicePo();
        rentalDevicePo.setId(req.getRentalId());
        rentalDevicePo.setEndDate(req.getEndDate());

        this.updateById(rentalDevicePo);
    }

    /**
     * 结束服务
     *
     * @param rentalId
     */
    @Override
    public void endService(String rentalId) {
        RentalDevicePo rentalDevicePo = new RentalDevicePo();
        rentalDevicePo.setId(rentalId);

        rentalDevicePo.setEndDate(new Date());

        this.updateById(rentalDevicePo);
    }

    /**
     * 删除租赁
     * @param rentalId
     */
    @Override
    public void delById(String rentalId) {
//        RentalDevicePo rentalDevicePo = new RentalDevicePo();
//        rentalDevicePo.setId(rentalId);
//        rentalDevicePo.setEndDate(new Date());
//        rentalDevicePo.setIsDelete(1);
//        this.updateById(rentalDevicePo);
        rentalMapper.del(rentalId);
    }

}
